package com.whfc.common.third.sms.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.third.sms.SmsApi;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 阿里云短信接口
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-10-13 10:45
 */
public class AliyunSmsApi implements SmsApi {

    private Logger logger = LoggerFactory.getLogger(AliyunSmsApi.class);

    /**
     * key
     */
    private String accessKeyId;

    /**
     * secret
     */
    private String accessSecret;

    /**
     * 短信签名
     */
    private String sign;

    /**
     * 短信模板
     */
    private Map<String, String> template;

    public AliyunSmsApi(String accessKeyId, String accessSecret, String sign, Map<String, String> template) {
        this.accessKeyId = accessKeyId;
        this.accessSecret = accessSecret;
        this.sign = sign;
        this.template = template;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public Map<String, String> getTemplate() {
        return template;
    }

    public void setTemplate(Map<String, String> template) {
        this.template = template;
    }

    @Override
    public String sendSmsVerify(String phone, String code) {
        String templateCode = template.get(SmsApi.TEMPLATE_CODE_VERIFY);
        Map<String, Object> params = new HashMap<>(1);
        params.put("code", code);
        return sendSms(phone, templateCode, params);
    }

    @Override
    public String sendSmsEmp(String phone, String username, Date time, String empName, String rule) {
        String templateCode = template.get(SmsApi.TEMPLATE_CODE_EMP);
        Map<String, Object> params = new HashMap<>(4);
        params.put("username", username);
        params.put("time", DateUtil.formatDateTime(time));
        params.put("rule", rule);
        params.put("empName", empName);
        return sendSms(phone, templateCode, params);
    }

    @Override
    public String sendSmsMach(String phone, String username, Date time, String deviceName, String rule) {
        String templateCode = template.get(SmsApi.TEMPLATE_CODE_MACH);
        Map<String, Object> params = new HashMap<>(4);
        params.put("username", username);
        params.put("time", DateUtil.formatDateTime(time));
        params.put("deviceName", deviceName);
        params.put("rule", rule);
        return sendSms(phone, templateCode, params);
    }

    @Override
    public String sendSmsFim(String phone, String username, Date time, String deviceName, String rule) {
        String templateCode = template.get(SmsApi.TEMPLATE_CODE_FIM);
        Map<String, Object> params = new HashMap<>(4);
        params.put("username", username);
        params.put("deviceName", deviceName);
        params.put("time", DateUtil.formatDateTime(time));
        params.put("rule", rule);
        return sendSms(phone, templateCode, params);
    }

    @Override
    public String sendSmsFse(String phone, String username, Date time, String deviceName, String rule) {
        String templateCode = template.get(SmsApi.TEMPLATE_CODE_FSE);
        Map<String, Object> params = new HashMap<>(4);
        params.put("username", username);
        params.put("rule", rule);
        params.put("deviceName", deviceName);
        params.put("time", DateUtil.formatDateTime(time));
        return sendSms(phone, templateCode, params);
    }

    @Override
    public String sendSmsEnv(String phone, String username, Date time, String deviceName, String rule) {
        String templateCode = template.get(SmsApi.TEMPLATE_CODE_ENV);
        Map<String, Object> params = new HashMap<>(4);
        params.put("username", username);
        params.put("rule", rule);
        params.put("time", DateUtil.formatDateTime(time));
        params.put("deviceName", deviceName);
        return sendSms(phone, templateCode, params);
    }

    @Override
    public String sendSmsRotary(String phone, String username, Date time, String deviceName, String rule) {
        String templateCode = template.get(SmsApi.TEMPLATE_CODE_ROTARY);
        Map<String, Object> params = new HashMap<>(4);
        params.put("username", username);
        params.put("time", DateUtil.formatDateTime(time));
        params.put("rule", rule);
        params.put("deviceName", deviceName);
        return sendSms(phone, templateCode, params);
    }

    @Override
    public String sendSms(String phone, String templateCode, Map<String, Object> templateParam) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", JSONUtil.toString(templateParam));
        request.putQueryParameter("SignName", sign);
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            logger.info("发送短信结果:{}", data);
            String code = JSONObject.parseObject(data).getString("Code");
            if ("OK".equals(code)) {
                return ResultEnum.SUCCESS.getCode();
            } else {
                return ResultEnum.FAILURE.getCode();
            }
        } catch (Exception e) {
            logger.error("发送短信失败", e);
            return ResultEnum.FAILURE.getCode();
        }
    }
}
